package cc.ufinity.networkdiagnose.manager;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.xmlpull.v1.XmlSerializer;

import android.app.Activity;
import android.net.ConnectivityManager;
import android.os.Environment;
import android.telephony.NeighboringCellInfo;
import android.util.Log;
import android.util.Xml;
import cc.ufinity.networkdiagnose.app.GlobalApp;

public class CreateResultXML {
	public static int a;
	public static int b;

	public CreateResultXML(){
		
	}
	/**
	 * 建立wlan测试的上传文件
	 * 
	 * @param task
	 * @return fileName
	 */
	@SuppressWarnings("finally")
	public String createWLANxml(Task task, Activity ctx) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
		Date curDate = new Date(System.currentTimeMillis());
		String fileName = "WLAN_" + formatter.format(curDate) + ".xml"; 
		File newxmlfile = null;
//------------------------------------------------------------------------------------	
		if(GlobalApp.DEBUG) {
//			try {
//	    	  File fileDir=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/wlantest/");
//	              fileDir.mkdir();
//			} catch (Exception e) {
//				// TODO: handle exception
//				return null;
//			}
////		        File fileDir=new File(Environment.getExternalStorageDirectory().getAbsolutePath());
////		           fileDir.mkdir();
//	        newxmlfile = new File(Environment.getExternalStorageDirectory() + "/wlantest/"+ fileName);
//	        Log.d("XmlFileCreator", "新的XML文件：" + newxmlfile.getName());
//	        try {
//	            if (!newxmlfile.exists())
//	                newxmlfile.createNewFile();
//	        } catch (IOException e) {
//	            Log.e("XmlFileCreator", "创建XML文件出错：", e);
//	        }
			File fileDir1 = ctx.getFilesDir();
			String path = fileDir1.getParent() + File.separator + "wlantest"
					+ File.separator;
			File fileDir2 = new File(path);
			fileDir2.mkdir();
			newxmlfile = new File(path + fileName);

			try {
				if (!newxmlfile.exists()) {
					newxmlfile.createNewFile();
				}
			} catch (IOException e) {
				Log.e("CreateWLANxml", "创建XML文件出错：", e);
				return null;
			}	
		} else {
			File fileDir1 = ctx.getFilesDir();
			String path = fileDir1.getParent() + File.separator + "wlantest"
					+ File.separator;
			File fileDir2 = new File(path);
			fileDir2.mkdir();
			newxmlfile = new File(path + fileName);

			try {
				if (!newxmlfile.exists()) {
					newxmlfile.createNewFile();
				}
			} catch (IOException e) {
				Log.e("CreateWLANxml", "创建XML文件出错：", e);
				return null;
			}
		}
			
//------------------------------------------------------------------------------------			
		
		
//------------------------------------------------------------------------------------	
		FileOutputStream fileos = null;
		try {
			fileos = new FileOutputStream(newxmlfile);
		} catch (FileNotFoundException e) {
			Log.e("XmlFileCreator", "写入XML文件出错：", e);
			return null;
		}
		
		XmlSerializer serializer = Xml.newSerializer();
		try {
			serializer.setOutput(fileos, "UTF-8");
			serializer.startDocument(null, Boolean.valueOf(true));

			serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output",
					true);
			serializer.startTag("", "Root");
			serializer.startTag("", "TestResultInfo");
			String taskId = (task.id == null || task.id.equals("")) ? task.testPhone : task.id;
			serializer.startTag(null, "TaskID");
			serializer.text(String.valueOf(taskId));
			serializer.endTag(null, "TaskID");

			String city = (GlobalApp.city == null || GlobalApp.city.equals("")) ? "":GlobalApp.city ;
			city = city.equals("")? ((GlobalApp.CITY == null || GlobalApp.CITY.equals("")) ? city:GlobalApp.CITY ) : city;
			System.out.println("city---------"+city);
			serializer.startTag("", "City");
			serializer.text(city);
			serializer.endTag("", "City");

			String town = (GlobalApp.town == null || GlobalApp.town.equals("")) ? "":GlobalApp.town ;
			serializer.startTag("", "Town");
			serializer.text(town);
			serializer.endTag("", "Town");

			String carrier = (task.carrier == null || task.carrier.equals("")) ? "":task.carrier ;
			if(task.scanResult == null) {
				carrier = "中国移动";
			} else {
				if(task.scanResult.SSID.equals(WlanManager.SSID_CMCC)
						|| task.scanResult.SSID.equals(WlanManager.SSID_CMCC_EDU)
						|| task.scanResult.SSID.equals(WlanManager.SSID_CMCC_AUTO)) {
					carrier = "中国移动";
				} else if(task.scanResult.SSID.equals(WlanManager.SSID_ChinaNet)) {
					carrier = "中国电信";
				} else if(task.scanResult.SSID.equals(WlanManager.SSID_ChinaUnicom1)
						|| task.scanResult.SSID.equals(WlanManager.SSID_ChinaUnicom2)) {
					carrier = "中国联通";
				} else {
					carrier = "中国移动";
				}
			}
			
			serializer.startTag("", "CommunicationCarriers");
			serializer.text(carrier);
			serializer.endTag("", "CommunicationCarriers");

			String testPhone = (task.testPhone == null || task.testPhone.equals("")) ? "0":task.testPhone ;
			serializer.startTag("", "TestPhone");
			serializer.text(testPhone);
			serializer.endTag("", "TestPhone");

			String mac = (task.mac == null || task.mac.equals("")) ? "0":task.mac ;
			serializer.startTag("", "MAC");
			serializer.text(mac);
			serializer.endTag("", "MAC");

			String lac = (task.lac == null || task.lac.equals("")) ? "0":task.lac;
			serializer.startTag("", "Lac");
			serializer.text(lac);
			serializer.endTag("", "Lac");

			String ci = (task.ci == null || task.ci.equals("")) ? "0":task.ci;
			serializer.startTag("", "Ci");
			serializer.text(ci);
			serializer.endTag("", "Ci");

			String taskAddress = (task.taskAddress == null || task.taskAddress.equals("")) ? "0":task.taskAddress ;
			serializer.startTag("", "SiteDescript");
			serializer.text(taskAddress);
			serializer.endTag("", "SiteDescript");

			String startsTime = (task.startsTime == null || task.startsTime.equals("")) ? "0":task.startsTime;
			serializer.startTag("", "StartTime");
			serializer.text(startsTime);
			serializer.endTag("", "StartTime");

			String endsTime = (task.endsTime == null || task.endsTime.equals("")) ? "0":task.endsTime ;
			serializer.startTag("", "EndTime");
			serializer.text(endsTime);
			serializer.endTag("", "EndTime");

			String relevanceAPAvgTime = (task.relevanceAPAvgTime == null || task.relevanceAPAvgTime.equals("")) ?"0": task.relevanceAPAvgTime;
			serializer.startTag("", "RelevanceAPAvgTime");
			serializer.text(relevanceAPAvgTime);
			serializer.endTag("", "RelevanceAPAvgTime");

			String apTestCount = (task.apTestCount == null || task.apTestCount.equals("")) ? "0":task.apTestCount ;
			serializer.startTag("", "APTestCount");
			serializer.text(apTestCount);
			serializer.endTag("", "APTestCount");

			String apTestSuccessCount = (task.apTestSuccessCount == null || task.apTestSuccessCount.equals("")) ? "0":task.apTestSuccessCount ;
			serializer.startTag("", "APTestSuccessCount");
			serializer.text(apTestSuccessCount);
			serializer.endTag("", "APTestSuccessCount");

			String rssi = (task.rssi == null || task.rssi.equals("")) ? "0" :task.rssi;
			serializer.startTag("", "RSSI");
			serializer.text(rssi);
			serializer.endTag("", "RSSI");

			String snr = (task.snr == null || task.snr.equals("")) ? "0" : task.snr;
			serializer.startTag("", "SNR");
			serializer.text(snr);
			serializer.endTag("", "SNR");

			if(task.scanResult.SSID.equals(WlanManager.SSID_CMCC)) {
			String webDelay = (task.webDelay == null || task.webDelay.equals("")) ? "0":task.webDelay ;
				serializer.startTag("", "WebDelayTime");
				serializer.text(webDelay);
				serializer.endTag("", "WebDelayTime");

			String webLoginCount = (task.webLoginCount == null || task.webLoginCount.equals("")) ? "0":task.webLoginCount ;
				serializer.startTag("", "WebLoginCount");
				serializer.text(webLoginCount);
				serializer.endTag("", "WebLoginCount");

			String webLoginSuccessCount = (task.webLoginSuccessCount == null || task.webLoginSuccessCount.equals("")) ?"0": task.webLoginSuccessCount;
				serializer.startTag("", "WebLoginSuccessCount");
				serializer.text(webLoginSuccessCount);
				serializer.endTag("", "WebLoginSuccessCount");

				String webOutCount = (task.webOutCount == null || task.webOutCount.equals("")) ? "0":task.webOutCount;
				serializer.startTag("", "WebOutCount");
				serializer.text(webOutCount);
				serializer.endTag("", "WebOutCount");
				
           String webOutSuccessCount = (task.webOutSuccessCount == null || task.webOutSuccessCount.equals("")) ? "0":task.webOutSuccessCount;
				serializer.startTag("", "WebOutSuccessCount");
				serializer.text(webOutSuccessCount);
				serializer.endTag("", "WebOutSuccessCount");
			} else {
				serializer.startTag("", "WebDelayTime");
				serializer.text("-1");
				serializer.endTag("", "WebDelayTime");

				serializer.startTag("", "WebLoginCount");
				serializer.text("-1");
				serializer.endTag("", "WebLoginCount");

				serializer.startTag("", "WebLoginSuccessCount");
				serializer.text("-1");
				serializer.endTag("", "WebLoginSuccessCount");

				serializer.startTag("", "WebOutCount");
				serializer.text("-1");
				serializer.endTag("", "WebOutCount");

				serializer.startTag("", "WebOutSuccessCount");
				serializer.text("-1");
				serializer.endTag("", "WebOutSuccessCount");
			}
			

			//if(task.scanResult.SSID.equals(WlanManager.SSID_CMCC_AUTO)) {
			if(GlobalApp.isPeapTested) {
				String peapDelay = (task.peapDelay == null || task.peapDelay.equals("")) ? "0":task.peapDelay;
				serializer.startTag("", "PeapDelay");
				serializer.text(peapDelay);
				serializer.endTag("", "PeapDelay");

				String peapCount = (task.peapCount == null || task.peapCount.equals("")) ? "0":task.peapCount ;
				serializer.startTag("", "PeapCount");
				serializer.text(peapCount);
				serializer.endTag("", "PeapCount");
				
String peapSuccessCount = (task.peapSuccessCount == null || task.peapSuccessCount.equals("")) ? "0":task.peapSuccessCount ;
				serializer.startTag("", "PeapSuccessCount");
				serializer.text(peapSuccessCount);
				serializer.endTag("", "PeapSuccessCount");

				String peapOffCount = (task.peapOffCount == null || task.peapOffCount.equals("")) ? "0":task.peapOffCount;
				serializer.startTag("", "PeapOffCount");
				serializer.text(peapOffCount);
				serializer.endTag("", "PeapOffCount");
String peapOffSuccessCount = (task.peapOffSuccessCount == null || task.peapOffSuccessCount.equals("")) ? "0":task.peapOffSuccessCount ;
				serializer.startTag("", "PeapOffSuccessCount");
				serializer.text(peapOffSuccessCount);
				serializer.endTag("", "PeapOffSuccessCount");
			} else {
				serializer.startTag("", "PeapDelay");
				serializer.text("-1");
				serializer.endTag("", "PeapDelay");

				serializer.startTag("", "PeapCount");
				serializer.text("-1");
				serializer.endTag("", "PeapCount");

				serializer.startTag("", "PeapSuccessCount");
				serializer.text("-1");
				serializer.endTag("", "PeapSuccessCount");

				serializer.startTag("", "PeapOffCount");
				serializer.text("-1");
				serializer.endTag("", "PeapOffCount");

				serializer.startTag("", "PeapOffSuccessCount");
				serializer.text("-1");
				serializer.endTag("", "PeapOffSuccessCount");
			}
			
String httpAnwerDelay = (task.httpAnwerDelay == null || task.httpAnwerDelay.equals("")) ? "0":task.httpAnwerDelay ;
			serializer.startTag(null, "HttpAnwerDelay");
			serializer.text(httpAnwerDelay);
			serializer.endTag(null, "HttpAnwerDelay");
			
String httpCallCount = (task.httpCallCount == null || task.httpCallCount.equals("")) ? "0":task.httpCallCount;
			serializer.startTag(null, "HttpCallCount");
			serializer.text(httpCallCount);
			serializer.endTag(null, "HttpCallCount");

 String httpCallSuccessCount = (task.httpCallSuccessCount == null || task.httpCallSuccessCount.equals("")) ?"0": task.httpCallSuccessCount ;
			serializer.startTag(null, "HttpCallSuccessCount");
			serializer.text(httpCallSuccessCount);
			serializer.endTag(null, "HttpCallSuccessCount");

			String pingAvgDelay = (task.pingAvgDelay == null || task.pingAvgDelay.equals("")) ? "0":task.pingAvgDelay ;
			serializer.startTag(null, "PingAvgDelay");
			serializer.text(pingAvgDelay);
			serializer.endTag(null, "PingAvgDelay");

			String pingCount = (task.pingCount == null || task.pingCount.equals("")) ? "0":task.pingCount ;
			serializer.startTag(null, "PingCount");
			serializer.text(pingCount);
			serializer.endTag(null, "PingCount");

			String pingSuccessCount = (task.pingSuccessCount == null || task.pingSuccessCount.equals("")) ? "0":task.pingSuccessCount;
			serializer.startTag(null, "PingSuccessCount");
			serializer.text(pingSuccessCount);
			serializer.endTag(null, "PingSuccessCount");

			String ftpUpCount = (task.ftpUpCount == null || task.ftpUpCount.equals("")) ? "0" : task.ftpUpCount;
			serializer.startTag(null, "FTPUpCount");
			serializer.text(ftpUpCount);
			serializer.endTag(null, "FTPUpCount");

			String ftpUpSuccessCount = (task.ftpUpSuccessCount == null || task.ftpUpSuccessCount.equals("")) ? "0":task.ftpUpSuccessCount;
			serializer.startTag(null, "FTPUpSuccessCount");
			serializer.text(ftpUpSuccessCount);
			serializer.endTag(null, "FTPUpSuccessCount");

			String ftpDownCount = (task.ftpDownCount == null || task.ftpDownCount.equals("")) ? "0":task.ftpDownCount ;
			serializer.startTag(null, "FTPDownCount");
			serializer.text(ftpDownCount);
			serializer.endTag(null, "FTPDownCount");

			String ftpDownSuccessCount = (task.ftpDownSuccessCount == null || task.ftpDownSuccessCount.equals("")) ?"0": task.ftpDownSuccessCount;
			serializer.startTag(null, "FTPDownSuccessCount");
			serializer.text(ftpDownSuccessCount);
			serializer.endTag(null, "FTPDownSuccessCount");

			String ftpDownAvgSuccessRate = (task.ftpDownAvgSuccessRate == null || task.ftpDownAvgSuccessRate.equals("")) ? "0":task.ftpDownAvgSuccessRate ;
			serializer.startTag(null, "FTPDownAvgSuccessRate");
			serializer.text(ftpDownAvgSuccessRate);
			serializer.endTag(null, "FTPDownAvgSuccessRate");

			String ftpUpAvgSuccessRate = (task.ftpUpAvgSuccessRate == null || task.ftpUpAvgSuccessRate.equals("")) ? "0":task.ftpUpAvgSuccessRate ;
			serializer.startTag(null, "FTPUpAvgSuccessRate");
			serializer.text(ftpUpAvgSuccessRate);
			serializer.endTag(null, "FTPUpAvgSuccessRate");

			String userIsolateCount = (task.userIsolateCount == null || task.userIsolateCount.equals("")) ? "0":task.userIsolateCount;
			serializer.startTag("", "UserIsolateCount");
			serializer.text(userIsolateCount);
			serializer.endTag("", "UserIsolateCount");

			String userIsolateSuccessCount = (task.userIsolateSuccessCount == null || task.userIsolateSuccessCount.equals("")) ?"0": task.userIsolateSuccessCount ;
			serializer.startTag("", "UserIsolateSuccessCount");
			serializer.text(userIsolateSuccessCount);
			serializer.endTag("", "UserIsolateSuccessCount");
			
String userRoamCount = (task.userRoamCount == null || task.userRoamCount.equals("")) ? "0":task.userRoamCount;
			serializer.startTag("", "UserRoamCount");
			serializer.text(userRoamCount);
			serializer.endTag("", "UserRoamCount");
			
String userRoamSuccessCount = (task.userRoamSuccessCount == null || task.userRoamSuccessCount.equals("")) ?"0": task.userRoamSuccessCount;
			serializer.startTag("", "UserRoamSuccessCount");
			serializer.text(userRoamSuccessCount);
			serializer.endTag("", "UserRoamSuccessCount");

			serializer.startTag("", "Time");
			serializer.text(formatter1.format(curDate));
			serializer.endTag("", "Time");

			serializer.endTag("", "TestResultInfo");
			serializer.endTag("", "Root");
			serializer.endDocument();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			System.out.println("返回fileName:================"+fileName);  
			return fileName;
		}
	}

	/**
	 * 建立GSM和TD的测试的上传文件
	 * 
	 * @param task
	 * @return fileName
	 */
	public String CreateGSM_TDxml(Task task, Activity ctx) {
		String name = "";
		if(task.selNetType == ConnectivityManager.TYPE_MOBILE_MMS) {
			name = "TD_";
		} else if(task.selNetType == ConnectivityManager.TYPE_MOBILE) {
			name = "GSM_";
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		Date curDate = new Date(System.currentTimeMillis());
		String fileName = name + formatter.format(curDate) + ".xml";
		
		SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");

		File newxmlfile = null;
//------------------------------------------------------------------------------------	
		if(GlobalApp.DEBUG) {
//			try {
//	    	  File fileDir=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/wlantest/");
//	          fileDir.mkdir();
//			} catch (Exception e) {
//				System.out.println("【1】\n" + e);
//				return null;
//			}
////		        File fileDir=new File(Environment.getExternalStorageDirectory().getAbsolutePath());
////		           fileDir.mkdir();
//	        newxmlfile = new File(Environment.getExternalStorageDirectory() + "/wlantest/"+ fileName);
//	        Log.d("XmlFileCreator", "新的XML文件：" + newxmlfile.getName());
//	        try {
//	            if (!newxmlfile.exists())
//	                newxmlfile.createNewFile();
//	        } catch (IOException e) {
//	        	System.out.println("【2】\n" + e);
//	        }
			File fileDir1 = ctx.getFilesDir();
			String path = fileDir1.getParent() + File.separator + "wlantest"
					+ File.separator;
			File fileDir2 = new File(path);
			fileDir2.mkdir();
			newxmlfile = new File(path + fileName);

			try {
				if (!newxmlfile.exists()) {
					newxmlfile.createNewFile();
				}
			} catch (IOException e) {
				System.out.println("【3】\n" + e);
				return null;
			}
		} else {
			File fileDir1 = ctx.getFilesDir();
			String path = fileDir1.getParent() + File.separator + "wlantest"
					+ File.separator;
			File fileDir2 = new File(path);
			fileDir2.mkdir();
			newxmlfile = new File(path + fileName);

			try {
				if (!newxmlfile.exists()) {
					newxmlfile.createNewFile();
				}
			} catch (IOException e) {
				System.out.println("【3】\n" + e);
				return null;
			}
		}
			
//------------------------------------------------------------------------------------			
		
		
//------------------------------------------------------------------------------------	
		FileOutputStream fileos = null;
		try {
			fileos = new FileOutputStream(newxmlfile);
		} catch (FileNotFoundException e) {
			System.out.println("【4】\n" + e);
		}

		XmlSerializer serializer = Xml.newSerializer();
		  
			try {
				serializer.setOutput(fileos, "UTF-8");
			serializer.startDocument(null, Boolean.valueOf(true));

			serializer.setFeature(
					"http://xmlpull.org/v1/doc/features.html#indent-output",
					true);

			serializer.startTag("", "Root");
			serializer.startTag("", "TestResultInfo");
			String taskId = (task.id == null || task.id.equals("")) ? task.testPhone
					: task.id;
			serializer.startTag(null, "TaskID");
			serializer.text(String.valueOf(taskId));
			serializer.endTag(null, "TaskID");

		//	String city = (task.city == null || task.city.equals("")) ? "0":task.city ;		
			String city = "";
//			if(GlobalApp.isWlanFirst) {
				city = (GlobalApp.city == null || GlobalApp.city.equals("")) ? "":GlobalApp.city ;
//			} else
//			{
//				city = (GetAPInfo.downApinfo2==null || GetAPInfo.downApinfo2.getCity() == null || GetAPInfo.downApinfo2.getCity().equals("")) ? "0":GetAPInfo.downApinfo2.getCity();
//			}
			city = city.equals("")? ((GlobalApp.CITY == null || GlobalApp.CITY.equals("")) ? city:GlobalApp.CITY ) : city;
			serializer.startTag("", "City");
			serializer.text(city);
			serializer.endTag("", "City");

		//	String town = (task.town == null || task.town.equals("")) ? "0":task.town;
			String town = "";
//			if(GlobalApp.isWlanFirst) {
				town = (GlobalApp.town == null || GlobalApp.town.equals("")) ? "":GlobalApp.town ;
//			}else
//			{
//				city = (GetAPInfo.downApinfo2==null || GetAPInfo.downApinfo2.getTown() == null || GetAPInfo.downApinfo2.getTown().equals("")) ? "0":GetAPInfo.downApinfo2.getTown();
//			}
			serializer.startTag("", "Town");
			serializer.text(town);
			serializer.endTag("", "Town");

			String testPhone = (task.testPhone == null || task.testPhone.equals("")) ?"": task.testPhone;	
			serializer.startTag("", "TestPhone");
			serializer.text(testPhone);
			serializer.endTag("", "TestPhone");

			String lac = (task.lac == null || task.lac.equals("")) ?"0": task.lac;
			serializer.startTag("", "Lac");
			serializer.text(lac);
			serializer.endTag("", "Lac");

			String ci = (task.ci == null || task.ci.equals("")) ? "0":task.ci ;
			serializer.startTag("", "Ci");
			serializer.text(ci);
			serializer.endTag("", "Ci");

			String taskAddress;
			if(GlobalApp.isWlanFirst) {
				taskAddress = (GlobalApp.taskWlan.taskAddress == null || GlobalApp.taskWlan.taskAddress.equals("")) 
						? "0":GlobalApp.taskWlan.taskAddress ;
			} else {
				taskAddress = (task.taskAddress == null || task.taskAddress.equals("")) ?"0":task.taskAddress ;
			}
			
			serializer.startTag("", "SiteDescription");
			serializer.text(taskAddress);
			serializer.endTag("", "SiteDescription");

			String startsTime = (task.startsTime == null || task.startsTime.equals("")) ? "0":task.startsTime;
			serializer.startTag("", "StartTime");
			serializer.text(startsTime);
			serializer.endTag("", "StartTime");

			String endsTime = (task.endsTime == null || task.endsTime.equals("")) ? "0":task.endsTime ;
			serializer.startTag("", "EndTime");
			serializer.text(endsTime);
			serializer.endTag("", "EndTime");

			String longitute = (task.longitute == null || task.longitute.equals("")) ? "0":task.longitute ;
			serializer.startTag("", "Longitude");
			serializer.text(longitute);
			serializer.endTag("", "Longitude");
			
String latitute = (task.latitute == null || task.latitute.equals("")) ? "0":task.latitute ;
			serializer.startTag("", "Latitude");
			serializer.text(latitute);
			serializer.endTag("", "Latitude");

			String rssi = (task.rssi == null || task.rssi.equals("") || task.rssi.equals("0")) ? "0": "-"+task.rssi ;
			serializer.startTag("", "RSSI");
			serializer.text(rssi);
			serializer.endTag("", "RSSI");

			String snr = (task.snr == null || task.snr.equals("")) ? "0":task.snr ;
			serializer.startTag("", "SNR");
			serializer.text(snr);
			serializer.endTag("", "SNR");

			String httpAnwerDelay = (task.httpAnwerDelay == null || task.httpAnwerDelay.equals("")) ?"0": task.httpAnwerDelay ;
			serializer.startTag(null, "HttpAnwerDelay");
			serializer.text(httpAnwerDelay);
			serializer.endTag(null, "HttpAnwerDelay");

			String httpCallSuccessRate = (task.httpCallSuccessRate == null || task.httpCallSuccessRate.equals("")) ? "0":task.httpCallSuccessRate;
			serializer.startTag(null, "HttpCallSuccessRate");
			serializer.text(httpCallSuccessRate);
			serializer.endTag(null, "HttpCallSuccessRate");

			String httpCallCount = (task.httpCallCount == null || task.httpCallCount.equals("")) ? "0":task.httpCallCount ;
			serializer.startTag(null, "HttpCallCount");
			serializer.text(httpCallCount);
			serializer.endTag(null, "HttpCallCount");

			String httpCallSuccessCount = (task.httpCallSuccessCount == null || task.httpCallSuccessCount.equals("")) ? "0":task.httpCallSuccessCount ;
			serializer.startTag(null, "HttpCallSuccessCount");
			serializer.text(httpCallSuccessCount);
			serializer.endTag(null, "HttpCallSuccessCount");

			String pingAvgDelay = (task.pingAvgDelay == null || task.pingAvgDelay.equals("")) ? "0":task.pingAvgDelay ;
			serializer.startTag(null, "PingAvgDelay");
			serializer.text(pingAvgDelay);
			serializer.endTag(null, "PingAvgDelay");

			String pingCount = (task.pingCount == null || task.pingCount.equals("")) ? "0":task.pingCount ;
			serializer.startTag(null, "PingCount");
			serializer.text(pingCount);
			serializer.endTag(null, "PingCount");

			String pingSuccessCount = (task.pingSuccessCount == null || task.pingSuccessCount.equals("")) ? "0":task.pingSuccessCount ;
			serializer.startTag(null, "PingSuccessCount");
			serializer.text(pingSuccessCount);
			serializer.endTag(null, "PingSuccessCount");

			String ftpUpCount = (task.ftpUpCount == null || task.ftpUpCount.equals("")) ?"0": task.ftpUpCount ;
			serializer.startTag(null, "FTPUpCount");
			serializer.text(ftpUpCount);
			serializer.endTag(null, "FTPUpCount");

			String ftpUpSuccessCount = (task.ftpUpSuccessCount == null || task.ftpUpSuccessCount.equals("")) ? "0":task.ftpUpSuccessCount ;
			serializer.startTag(null, "FTPUpSuccessCount");
			serializer.text(ftpUpSuccessCount);
			serializer.endTag(null, "FTPUpSuccessCount");

			String ftpDownCount = (task.ftpDownCount == null || task.ftpDownCount.equals("")) ? "0":task.ftpDownCount;
			serializer.startTag(null, "FTPDownCount");
			serializer.text(ftpDownCount);
			serializer.endTag(null, "FTPDownCount");

			String ftpDownSuccessCount = (task.ftpDownSuccessCount == null || task.ftpDownSuccessCount.equals("")) ? "0":task.ftpDownSuccessCount ;
			serializer.startTag(null, "FTPDownSuccessCount");
			serializer.text(ftpDownSuccessCount);
			serializer.endTag(null, "FTPDownSuccessCount");

			String ftpDownAvgSuccessRate = (task.ftpDownAvgSuccessRate == null || task.ftpDownAvgSuccessRate.equals("")) ? "0":task.ftpDownAvgSuccessRate ;
			serializer.startTag(null, "FTPDownAvgSuccessRate");
			serializer.text(ftpDownAvgSuccessRate);
			serializer.endTag(null, "FTPDownAvgSuccessRate");

			String ftpUpAvgSuccessRate = (task.ftpUpAvgSuccessRate == null || task.ftpUpAvgSuccessRate.equals("")) ?"0": task.ftpUpAvgSuccessRate ;
			serializer.startTag(null, "FTPUpAvgSuccessRate");
			serializer.text(ftpUpAvgSuccessRate);
			serializer.endTag(null, "FTPUpAvgSuccessRate");

			serializer.startTag("", "Time");
			serializer.text(formatter1.format(curDate));
			serializer.endTag("", "Time");

			serializer.endTag("", "TestResultInfo");
			serializer.endTag("", "Root");
			serializer.endDocument();
			
			return fileName;
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * TD和GSM
	 * @param task
	 * @param ctx
	 * @return fileName
	 */
	public String CreateCELLxml(Task task, Activity ctx) {

		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		Date curDate = new Date(System.currentTimeMillis());
		String fileName = "CELL_" + formatter.format(curDate) + ".xml";
		SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");

		File fileDir1 = ctx.getFilesDir();
		String path = fileDir1.getParent() + File.separator + "wlantest"
				+ File.separator;
		File fileDir2 = new File(path);
		fileDir2.mkdir();
		File newxmlfile = new File(path + fileName);

		try {
			if (!newxmlfile.exists()) {
				newxmlfile.createNewFile();
			}
		} catch (IOException e) {
			Log.e("CreateWLANxml", "创建XML文件出错：", e);
			return null;
		}

		FileOutputStream fileos = null;
		try {
			fileos = new FileOutputStream(newxmlfile);
		} catch (FileNotFoundException e) {
			Log.e("XmlFileCreator", "写入XML文件出错：", e);
		}

		XmlSerializer serializer = Xml.newSerializer();
		try {

			serializer.setOutput(fileos, "UTF-8");
			serializer.startDocument(null, Boolean.valueOf(true));

			serializer.setFeature(
					"http://xmlpull.org/v1/doc/features.html#indent-output",
					true);

			serializer.startTag("", "Root");

			String taskId = (task.id == null || task.id.equals("")) ? task.testPhone
					: task.id;
			serializer.startTag(null, "TaskID");
			serializer.text(String.valueOf(taskId));
			serializer.endTag(null, "TaskID");

			serializer.startTag("", "TestResultInfo");

			 
			serializer.startTag("", "NetType");
			serializer.text(""+task.selNetType);
			serializer.endTag("", "NetType");

			String rssi = (task.rssi == null || task.rssi.equals("") || task.rssi.equals("0")) ? "0" : "-"+task.rssi;					
			serializer.startTag("", "RSSI");
			serializer.text(rssi);
			serializer.endTag("", "RSSI");

			String lac = (task.lac == null || task.lac.equals("")) ? "0":task.lac ;	
			serializer.startTag("", "Lac");
			serializer.text(lac);
			serializer.endTag("", "Lac");

			String ci = (task.ci == null || task.ci.equals("")) ? "0":task.ci ;	
			serializer.startTag("", "Ci");
			serializer.text(ci);
			serializer.endTag("", "Ci");			
			
			serializer.startTag("", "Time");
			serializer.text(formatter1.format(curDate));
			serializer.endTag("", "Time");

			serializer.endTag("", "TestResultInfo");
			serializer.startTag("", "TestResultInfo");

			serializer.startTag("", "NetType");
			serializer.text(""+task.selNetType);
			serializer.endTag("", "NetType");

			String rssii = (task.rssi == null || task.rssi.equals("") || task.rssi.equals("0")) ? "0": "-"+task.rssi;					
			serializer.startTag("", "RSSI");
			serializer.text(rssii);
			serializer.endTag("", "RSSI");

			String lac1 = (task.lac == null || task.lac.equals("")) ? "0":task.lac ;	
			serializer.startTag("", "Lac");
			serializer.text(lac1);
			serializer.endTag("", "Lac");

			String ci1 = (task.ci == null || task.ci.equals("")) ? "0":task.ci ;	
			serializer.startTag("", "Ci");
			serializer.text(ci1);
			serializer.endTag("", "Ci");	
			
			serializer.startTag("", "Time");
			serializer.text(formatter1.format(curDate));
			serializer.endTag("", "Time");

			serializer.endTag("", "TestResultInfo");
			serializer.endTag("", "Root");
			serializer.endDocument();
			
			return fileName;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	
	
	
	
	
	
	/**
	 * 建立邻居小区号测试的上传文件
	 * 
	 * @param task
	 * @return fileName
	 */
	public String CreateNeighborCellxml(Task task, Activity ctx) {
		
		String name = "CELL_"; 
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		Date curDate = new Date(System.currentTimeMillis());
		String fileName = name + formatter.format(curDate) + ".xml";
		
		SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
 
		File newxmlfile = null;
//------------------------------------------------------------------------------------		
		if(GlobalApp.DEBUG) {
//			try {
//	    	  File fileDir=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/wlantest/");
//	              fileDir.mkdir();
//			} catch (Exception e) {
//				System.out.println("【1】\n" + e);
//				return null;
//			}
//
//	        newxmlfile = new File(Environment.getExternalStorageDirectory() + "/wlantest/"+ fileName);
//	        Log.d("XmlFileCreator", "新的XML文件：" + newxmlfile.getName());
//	        try {
//	            if (!newxmlfile.exists())
//	                newxmlfile.createNewFile();
//	        } catch (IOException e) {
//	        	System.out.println("【2】\n" + e);
//	        }
			
			File fileDir1 = ctx.getFilesDir();
			String path = fileDir1.getParent() + File.separator + "wlantest"
					+ File.separator;
			File fileDir2 = new File(path);
			fileDir2.mkdir();
			newxmlfile = new File(path + fileName);

			try {
				if (!newxmlfile.exists()) {
					newxmlfile.createNewFile();
				}
			} catch (IOException e) { 
				return null;
			}
	        
		} else {
			File fileDir1 = ctx.getFilesDir();
			String path = fileDir1.getParent() + File.separator + "wlantest"
					+ File.separator;
			File fileDir2 = new File(path);
			fileDir2.mkdir();
			newxmlfile = new File(path + fileName);

			try {
				if (!newxmlfile.exists()) {
					newxmlfile.createNewFile();
				}
			} catch (IOException e) { 
				return null;
			}
		}
		
//------------------------------------------------------------------------------------	
        
		
//------------------------------------------------------------------------------------	
		FileOutputStream fileos = null;
		try {
			fileos = new FileOutputStream(newxmlfile);
		} catch (FileNotFoundException e) { 
		}

		XmlSerializer serializer = Xml.newSerializer();
		  
			try {
				serializer.setOutput(fileos, "UTF-8");
			serializer.startDocument(null, Boolean.valueOf(true));

			serializer.setFeature(
					"http://xmlpull.org/v1/doc/features.html#indent-output",
					true);  
			serializer.startTag("", "Root");
			String taskId = (task.id == null || task.id.equals("")) ? task.testPhone
					: task.id;
			serializer.startTag(null, "TaskID");
			serializer.text(String.valueOf(taskId));
			serializer.endTag(null, "TaskID");
			if (task.neighborInfos != null) {
				System.out.println(">>>>>>>>>>>>>>>>>>"+task.neighborInfos .size());
				for (NeighboringCellInfo neighboringCellInfo : task.neighborInfos ) {  
					serializer.startTag("", "TestResultInfo"); 

					String rssi = (task.rssi == null || task.rssi.equals("") || task.rssi.equals("0")) ? "0": task.rssi;
					if(!rssi.equals("0") && !rssi.startsWith("-")) {
						rssi = "-" + rssi;
					}
					serializer.startTag("", "Rssi");
					serializer.text(rssi);
					serializer.endTag("", "Rssi"); 
					 
					serializer.startTag("", "Lac");
					serializer.text(String.valueOf(neighboringCellInfo.getLac()));
					serializer.endTag("", "Lac");

					serializer.startTag("", "Ci");
					serializer.text(String.valueOf(neighboringCellInfo.getCid()));
					serializer.endTag("", "Ci");

					serializer.startTag("", "Time");
					serializer.text(formatter1.format(curDate));
					serializer.endTag("", "Time");
					serializer.endTag("", "TestResultInfo");
				}
			} else {
				System.out.println("++++++++++++++++++++++++===没有相邻小区号");
			}  
			serializer.endTag("", "Root");
			serializer.endDocument();
			
			return fileName;
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
